package com.zf.shuashua.ui.home;

import android.Manifest;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.vise.xsnow.http.callback.ACallback;
import com.zf.shuashua.MainActivity;
import com.zf.shuashua.R;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.AbsCallback;
import com.zf.shuashua.api.Constants;
import com.zf.shuashua.api.HttpData;
import com.zf.shuashua.api.Store;
import com.zf.shuashua.bean.AddBlack;
import com.zf.shuashua.bean.BaseData;
import com.zf.shuashua.bean.ForwardIncreaseBean;
import com.zf.shuashua.manager.track.TrackConstants;
import com.zf.shuashua.manager.track.TrackManager;
import com.zf.shuashua.ui.mine.LogUtil;
import com.zf.shuashua.utils.ToastUtil;
import com.zf.shuashua.video.VideoPlayActivity;

import androidx.annotation.NonNull;

public class ShareVideoPopup extends BottomPopupView implements View.OnClickListener {
    private Context mContext;
    private String mTitle;
    private String mDescription;
    private String mShareUrl;
    private Activity mContext1;

    private LinearLayout reportLayout;

    public ShareVideoPopup(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public ShareVideoPopup(@NonNull Context context, String title, String description, String shareUrl) {
        super(context);
        this.mContext = context;
        this.mTitle = title;
        this.mDescription = description;
        this.mShareUrl = shareUrl;

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.share_bottom_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                reportAdvertAction(TrackConstants.ACTION_HOME_SHARE_RETURN, TrackConstants.NOTE_HOME_SHARE_RETURN);
                dismiss();
            }
        });
        findViewById(R.id.share_to_wechat).setOnClickListener(this);
        findViewById(R.id.share_to_qq).setOnClickListener(this);
        findViewById(R.id.share_to_qqzone).setOnClickListener(this);
        findViewById(R.id.share_to_weibo).setOnClickListener(this);
        findViewById(R.id.share_to_wechat_moment).setOnClickListener(this);
        findViewById(R.id.copy_link).setOnClickListener(this);
        reportLayout = findViewById(R.id.jubao);
        reportLayout.setOnClickListener(this);
    }

    private void reportAdvertAction(String action, String note) {
        TrackManager.getInstance().reportAdvertAction(TrackConstants.PAGE_ID_HOME, action, note);
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getWindowHeight(getContext()) * .85f);
    }


    public void getDomain(SHARE_MEDIA type) {
        if (mContext.getClass().equals(MainActivity.class)) {
            mContext1 = (MainActivity) mContext;
        } else {
            mContext1 = (VideoPlayActivity) mContext;
        }
        HttpData.getInstance().domain(new ACallback<BaseData>() {
            @Override
            public void onSuccess(BaseData data) {
                if (data.getCode() == 200 && data.getData() != null) {
//                    UMWeb web = new UMWeb(((String) data.getData()) + "/&/shareqq" + "?code=" + Store.getInstance().getAgent_code());//网址
//                    web.setTitle("我用 长豆短视频 看视频");//标题
//                    web.setThumb(new UMImage(mContext1, R.mipmap.ic_launcher));  //缩略图
//                    web.setDescription("看视频赚零花，能提现才好玩！");//描述
//                    UMWeb web = new UMWeb(Constants.H5_URL + "video/?id=" + mShareUrl);//网址
                    UMWeb web = new UMWeb(((String) data.getData()) + "/#/video/?id=" + mShareUrl);//网址
//                    UMVideo web = new UMVideo(((String) data.getData()) + "/&/video/?id=" + mShareUrl);//网址
                    web.setTitle(mTitle);//标题
                    web.setThumb(new UMImage(mContext1, R.mipmap.share_icon));  //缩略图
                    web.setDescription(mDescription);//描述

                    new ShareAction(mContext1)
                            .setPlatform(type)//传入平台
                            .withMedia(web)
                            .setCallback(shareListener)
                            .share();

                } else {
                    UMWeb web = new UMWeb(Constants.H5_YYB);//网址
                    web.setTitle("我用 长豆短视频 看视频");//标题
                    web.setThumb(new UMImage(mContext1, R.mipmap.share_icon));  //缩略图
                    web.setDescription("看视频赚零花，能提现才好玩！");//描述
                    new ShareAction(mContext1)
                            .setPlatform(type)//传入平台
                            .withMedia(web)
                            .setCallback(shareListener)
                            .share();
                }
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                UMWeb web = new UMWeb(Constants.H5_YYB);//网址
                web.setTitle("我用 长豆短视频 看视频");//标题
                web.setThumb(new UMImage(mContext1, R.mipmap.share_icon));  //缩略图
                web.setDescription("看视频赚零花，能提现才好玩！");//描述
                new ShareAction(mContext1)
                        .setPlatform(type)//传入平台
                        .withMedia(web)
                        .setCallback(shareListener)
                        .share();
            }
        });
    }


    @Override
    public void onClick(View v) {
        dismiss();
//        UMWeb web = new UMWeb(Constants.H5_URL + "video/?id=" + mShareUrl);//网址
//        web.setTitle(mTitle);//标题
//        web.setThumb(new UMImage(mContext, R.mipmap.ic_launcher));  //缩略图
//        web.setDescription(mDescription);//描述

        if (mContext.getClass().equals(MainActivity.class)) {
            mContext1 = (MainActivity) mContext;
        } else {
            mContext1 = (VideoPlayActivity) mContext;
        }

        LogUtil.d(mContext.getClass().equals(MainActivity.class) + "");
        switch (v.getId()) {
            case R.id.share_to_wechat://微信
                getDomain(SHARE_MEDIA.WEIXIN);
                reportAdvertAction(TrackConstants.ACTION_HOME_SHARE_WECHAT, TrackConstants.NOTE_HOME_SHARE_WECHAT);

                break;
            case R.id.share_to_qq:
                if (PermissionsUtil.hasPermission(mContext1, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    //有访问摄像头的权限
                    getDomain(SHARE_MEDIA.QQ);
                } else {
                    PermissionsUtil.requestPermission(mContext1, new PermissionListener() {
                        @Override
                        public void permissionGranted(@NonNull String[] permissions) {
                            //用户授予了
                            getDomain(SHARE_MEDIA.QQ);
                        }

                        @Override
                        public void permissionDenied(@NonNull String[] permissions) {
                            //用户拒绝了
                        }
                    }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
                }
                reportAdvertAction(TrackConstants.ACTION_HOME_SHARE_QQ, TrackConstants.NOTE_HOME_SHARE_QQ);

                break;
            case R.id.share_to_qqzone:
                if (PermissionsUtil.hasPermission(mContext1, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    //有访问摄像头的权限
                    getDomain(SHARE_MEDIA.QZONE);
                } else {
                    PermissionsUtil.requestPermission(mContext1, new PermissionListener() {
                        @Override
                        public void permissionGranted(@NonNull String[] permissions) {
                            //用户授予了
                            getDomain(SHARE_MEDIA.QZONE);
                        }

                        @Override
                        public void permissionDenied(@NonNull String[] permissions) {
                            //用户拒绝了
                        }
                    }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
                }
                reportAdvertAction(TrackConstants.ACTION_HOME_SHARE_QZONE, TrackConstants.NOTE_HOME_SHARE_QZONE);

                break;
            case R.id.share_to_weibo://微博
                getDomain(SHARE_MEDIA.SINA);
                reportAdvertAction(TrackConstants.ACTION_HOME_SHARE_BLOG, TrackConstants.NOTE_HOME_SHARE_BLOG);
                break;

            case R.id.share_to_wechat_moment://微信盆友圈
                getDomain(SHARE_MEDIA.WEIXIN_CIRCLE);
                reportAdvertAction(TrackConstants.ACTION_HOME_SHARE_FRIENDS, TrackConstants.NOTE_HOME_SHARE_FRIENDS);
                break;
            case R.id.copy_link:
                // 得到剪贴板管理器
                ClipboardManager cmb1 = (ClipboardManager) ShuaApplication.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
                HttpData.getInstance().domain(new ACallback<BaseData>() {
                    @Override
                    public void onSuccess(BaseData data) {
                        if (data.getCode() == 200 && data.getData() != null) {
                            cmb1.setPrimaryClip(ClipData.newPlainText(null, ((String) data.getData()) + "/#/shareqq" + "?code=" + Store.getInstance().getAgent_code()));
                        } else {
                            cmb1.setPrimaryClip(ClipData.newPlainText(null, Constants.H5_YYB));
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        cmb1.setPrimaryClip(ClipData.newPlainText(null, Constants.H5_YYB));

                    }
                });
                Toast.makeText(mContext, "复制成功", Toast.LENGTH_SHORT).show();
                forwardIncrease();
                reportAdvertAction(TrackConstants.ACTION_HOME_SHARE_COPYLINK, TrackConstants.NOTE_HOME_SHARE_COPYLINK);

                break;
            case R.id.jubao:
                new XPopup.Builder(mContext1)
                        .asBottomList("请选择举报类型", new String[]{"广告欺诈", "淫秽色情", "骚扰谩骂", "反动政治", "取消"},
                                null, -1,
                                new OnSelectListener() {
                                    @Override
                                    public void onSelect(int position, String text) {
                                        if (position == 4)
                                            return;//用户取消时return

                                        HttpData.getInstance().addReport(Integer.toString(position + 1), "video", mShareUrl, new ACallback<AddBlack>() {
                                            @Override
                                            public void onSuccess(AddBlack data) {
                                                if (data.getCode() == 200) {
                                                    ToastUtil.show(mContext1, "举报成功");
                                                }
                                            }

                                            @Override
                                            public void onFail(int errCode, String errMsg) {

                                            }
                                        });

                                    }
                                })
                        .show();
                break;


        }
    }

    private void shareToQQ() {
        if (PermissionsUtil.hasPermission(mContext1, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            //有访问摄像头的权限
            UMWeb web = new UMWeb(Constants.H5_YYB);//网址
            web.setTitle("我用 长豆短视频 看视频");//标题
            web.setThumb(new UMImage(mContext1, R.mipmap.ic_launcher));  //缩略图
            web.setDescription("看视频赚零花，能提现才好玩！");//描述
            new ShareAction(mContext1)
                    .setPlatform(SHARE_MEDIA.QQ)//传入平台
                    .withMedia(web)
                    .setCallback(shareListener)
                    .share();
        } else {
            PermissionsUtil.requestPermission(mContext1, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permissions) {
                    //用户授予了
                    UMWeb web = new UMWeb(Constants.H5_YYB);//网址
                    web.setTitle("我用 长豆短视频 看视频");//标题
                    web.setThumb(new UMImage(mContext1, R.mipmap.ic_launcher));  //缩略图
                    web.setDescription("看视频赚零花，能提现才好玩！");//描述
                    new ShareAction(mContext1)
                            .setPlatform(SHARE_MEDIA.QQ)//传入平台
                            .withMedia(web)
                            .setCallback(shareListener)
                            .share();
                }

                @Override
                public void permissionDenied(@NonNull String[] permissions) {
                    //用户拒绝了
                }
            }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
        }
    }

    //微博
    public void shareToSINA() {
        UMWeb web = new UMWeb(Constants.H5_URL + "shareqq" + "?code=" + Store.getInstance().getAgent_code());//网址
//            UMWeb web = new UMWeb(Constants.H5_YYB);//网址
        web.setTitle("我用 长豆短视频 看视频");//标题
        web.setThumb(new UMImage(mContext1, R.mipmap.ic_launcher));  //缩略图
        web.setDescription("看视频赚零花，能提现才好玩！");//描述
        new ShareAction(mContext1)
                .setPlatform(SHARE_MEDIA.SINA)//传入平台
                .withMedia(web)
                .setCallback(shareListener)
                .share();
    }

    //QQ空间
    @JavascriptInterface
    public void shareToQZONE() {
        if (PermissionsUtil.hasPermission(mContext1, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            //有访问摄像头的权限
            UMWeb web = new UMWeb(Constants.H5_YYB);//网址
            web.setTitle("我用 长豆短视频 看视频");//标题
            web.setThumb(new UMImage(mContext1, R.mipmap.ic_launcher));  //缩略图
            web.setDescription("看视频赚零花，能提现才好玩！");//描述
            new ShareAction(mContext1)
                    .setPlatform(SHARE_MEDIA.QZONE)//传入平台
                    .withMedia(web)
                    .setCallback(shareListener)
                    .share();
        } else {
            PermissionsUtil.requestPermission(mContext1, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permissions) {
                    //用户授予了
                    UMWeb web = new UMWeb(Constants.H5_YYB);//网址
                    web.setTitle("我用 长豆短视频 看视频");//标题
                    web.setThumb(new UMImage(mContext1, R.mipmap.ic_launcher));  //缩略图
                    web.setDescription("看视频赚零花，能提现才好玩！");//描述
                    new ShareAction(mContext1)
                            .setPlatform(SHARE_MEDIA.QZONE)//传入平台
                            .withMedia(web)
                            .setCallback(shareListener)
                            .share();
                }

                @Override
                public void permissionDenied(@NonNull String[] permissions) {
                    //用户拒绝了
                }
            }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
        }


    }

    //微信盆友圈
    public void shareToWEIXIN_CIRCLE() {
        UMWeb web = new UMWeb(Constants.H5_YYB);//网址
        web.setTitle("我用 长豆短视频 看视频");//标题
        web.setThumb(new UMImage(mContext1, R.mipmap.ic_launcher));  //缩略图
        web.setDescription("看视频赚零花，能提现才好玩！");//描述
        new ShareAction(mContext1)
                .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                .withMedia(web)
                .setCallback(shareListener)
                .share();
    }


    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //只要跳出qq/微信就分享成功
            forwardIncrease();
//            if (Constants.DAY_FIRSTSHARE) {
//                Constants.DAY_FIRSTSHARE = false;
//                HttpData.getInstance().reportGiftAction(GiftReport.EventType.day_firstShare, new ACallback<BaseData>() {
//                    @Override
//                    public void onSuccess(BaseData data) {
//                    }
//
//                    @Override
//                    public void onFail(int errCode, String errMsg) {
//                    }
//                });
//            }
        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            LogUtil.d("分享成功onStart");
            Toast.makeText(mContext, "分享成功啦", Toast.LENGTH_SHORT).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(mContext, "分享失败啦", Toast.LENGTH_SHORT).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            Toast.makeText(mContext, "分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    /**
     * 分享成功视频转发数+1
     */
    private void forwardIncrease() {
        HttpData.getInstance().toForwardIncrease(new AbsCallback<BaseData<ForwardIncreaseBean>>() {
            @Override
            public void onRealSuccess(BaseData<ForwardIncreaseBean> data) {
                LogUtil.d("分享成功");
            }

            @Override
            public void onFail(int errCode, String errMsg) {
                LogUtil.d("分享失败");
            }
        });
    }
}

